import es6 from 'es6-promise'

function httpRequest(obj, cashe) {
  return new es6.Promise((resolve, reject) => {
    obj.success = (res) => {
      wx.setStorageSync('http-cashe-' + obj.url, res)
      resolve(res)
    }
    obj.fail = (res) => reject(res)
    wx.request(obj)
  })
}

function get(url, data, cashe) {
  return httpRequest({
    url,
    data,
    header: {
      "Content-Type": "application/x-www-form-urlencoded",
      /* 'content-type': 'application/json', */ // 默认值
    },
    dataType: 'json',
  }, cashe)
}

function post(url, data, cashe) {
  return httpRequest({
    url,
    data,
    method: 'POST',
    header: {
      "Content-Type": "application/x-www-form-urlencoded",
      /* 'content-type': 'application/json', */ // 默认值
    }
  }, cashe)
}

export default httpRequest
export {
  get,
  post
}
